import requests
import re
import urllib3;urllib3.disable_warnings()


# 进入帖子的接口
def test_001(page,rows):
    url1="https://yuchengbaby.com/api/ForumService/v1/MemberForumResource/getMyPublishResourceList"
    # JSESSIONID1='18406c12-55a2-4586-a7eb-6c548d8c7dff&page=1&rows=15'
    # payload1={
    #     'SESSION':JSESSIONID1
    # }
    page = page
    rows = rows
    payload1 = {"SESSION": "18406c12-55a2-4586-a7eb-6c548d8c7dff",
                "page": page,
                "rows": rows}
    headers1={
        # 'Cookie: SESSION':'18406c12-55a2-4586-a7eb-6c548d8c7dff',
        'Cookie':'SESSION=18406c12-55a2-4586-a7eb-6c548d8c7dff',
        # 'Content-Length':'59',
        # 'Content-Type':'application / x - www - form - urlencoded',
        'Content-Type':'application/x-www-form-urlencoded',
        'Host':'yuchengbaby.com',
        # 'Connection':'Keep - Alive'
        'Connection':'Keep-Alive'
    }
    # ress=requests.get(url1,payload1,headers1=headers1)
    ress=requests.post(url1, payload1, headers=headers1, verify=False)
    res_text = ress.text
    list = re.findall(r'"id":"(.+?)",', res_text)
    print(payload1)
    print(res_text)
    print('id个数为：%s' % len(list))
    print(list)
    return list, len(list)

# 删除接口
def test_002(id):
    id = id
    url="https://yuchengbaby.com/api/ForumService/v1/MemberForumResource/delete"
    # JSESSIONID ='18406c12-55a2-4586-a7eb-6c548d8c7dff'
    # payload={
    #     "JSESSIONID":JSESSIONID,
    #
    # }
    payload1 = {"SESSION": "18406c12-55a2-4586-a7eb-6c548d8c7dff",
                "id": id}
    headers = {
        # 'Cookie: SESSION':'18406c12-55a2-4586-a7eb-6c548d8c7dff',
        'Cookie': 'SESSION=18406c12-55a2-4586-a7eb-6c548d8c7dff',
        # 'Content-Length':'59',
        # 'Content-Type':'application / x - www - form - urlencoded',
        'Content-Type': 'application/x-www-form-urlencoded',
        'Host': 'yuchengbaby.com',
        # 'Connection':'Keep - Alive'
        'Connection': 'Keep-Alive'
    }
    res= requests.post(url, payload1, headers=headers, verify=False)
    res_text = res.text
    print(res_text)

if __name__ == '__main__':
    # 循环三次
    for i in range(3):
        page = 1
        rows = 15
        """
        估计获取帖子id的接口，是根据页面（page），行数(rows)来获取不同的帖子id;
        这里是循环一次就行数（rows）+1；
        """
        id_list, len_id = test_001(page, rows)
        """
        test_001()返回了两个结果，一个是id列表，一个id长度；
        当长度为0，也就是没有获取到id就会断开
        """
        rows  = rows + 1
        if len_id < 1 :
            print('获取的id数量为0，结束循环')
            break
        for i in id_list:
            # 循环删除帖子，i为帖子id
            test_002(i)